package al.components;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Popup;
import javax.swing.PopupFactory;

import org.jdesktop.swingx.JXFrame;

import al.jdbc.DAO.Gestion_courrierDAO;
import al.jdbc.DAO.OrganigrammeDAO;
import al.jdbc.dpbridge.MySqlDataSource;
import alOmrane.jdbc.beans.Organigramme;

public class OrgPopup extends JPanel{
	private JTextField orgT = new JTextField();
	private JButton launchOrg = new JButton("+");
	private OrgD org = new OrgD();
	private JFrame frame;
	private OrganigrammeDAO odao = Gestion_courrierDAO.getInstance().getOrganigrammeDao();
	public OrgPopup(JFrame frame) {
		super();
		this.frame = frame;
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addComponent(orgT, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addComponent(launchOrg, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(launchOrg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	            .addComponent(orgT, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	        );
	    
	    org.setTextField(orgT);
	    launchOrg.addActionListener(new ShowPopupActionListener(frame,org,launchOrg));
	}
	public alOmrane.jdbc.beans.Organigramme getOrganigramme(){
		if(!orgT.getText().equals("")){
			try {
				alOmrane.jdbc.beans.Organigramme m = odao.findById(Integer.parseInt(orgT.getText()));
				return m;
			} catch (NumberFormatException e) {
				return null;
			}
		}else 
			return null;
	}
	public void setOrganigramme(Organigramme o){
		orgT.setText(""+o.getId());
	}
	public void reset(){
		this.orgT.setText("");
	}
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JXFrame frame = new JXFrame();
					OrgPopup fc = new OrgPopup(frame);
					fc.setVisible(true);
					frame.getContentPane().add(fc);
					frame.pack();
					frame.setVisible(true);
					frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	static class ShowPopupActionListener implements ActionListener {
		private Component component;
		private OrgD org;
		private JButton b;

		ShowPopupActionListener(Component component,OrgD org,JButton b) {
			this.component = component;
			this.org = org;
			this.b=b;
		}

		public synchronized void actionPerformed(ActionEvent actionEvent) {
			PopupFactory factory = PopupFactory.getSharedInstance();
			Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
			int width = org.getPreferredSize().width;
			int height = org.getPreferredSize().height;
			
			int x1 = b.getLocationOnScreen().x+width-dim.width;
			int y1 = b.getLocationOnScreen().y+height-dim.height;
			int x = x1<0?b.getLocationOnScreen().x:b.getLocationOnScreen().x-x1;
			int y = y1<0?b.getLocationOnScreen().y:b.getLocationOnScreen().y-y1;
			
			final Popup popup = factory.getPopup(component, org,x,y+b.getHeight());
			popup.show();
			b.setEnabled(false);
			org.setPopup(popup);
			org.setFrame((JFrame)component);
			org.setLauncher(b);
		}
	}
	

}
